package com.ford.trade.jingcai;

import com.ford.trade.entity.JcGame;
import com.ford.trade.entity.JingCaiPeiLv;
import lombok.Data;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

/**
 * @author: ford
 * @date: 2022/4/23 13:41
 */
@Data
@Component
public class JingCaiPeilvProcessor implements PageProcessor {

    private JingCaiPeilvService jingCaiPeilvService;
    private JcGame500Service jcGame500Service;

    private String type;
    private String date;
    private String cookie;
    private List<JcGame> games;
    private Map<String, JingCaiPeiLv> map = new HashMap<>();

    @Override
    public void process(Page page) {
        JingCaiPeiLv peiLv = new JingCaiPeiLv();
        Document document = page.getHtml().getDocument();
        String url = page.getUrl().get();
        //获取欧赔
        String companyType = "";
        Long indexId = Long.valueOf(url.substring(url.lastIndexOf("-") + 1, url.lastIndexOf(".")));
        if (url.contains("ouzhi")) {
            List<JingCaiPeiLv> peiLv2s = jingCaiPeilvService.findByIndexId(indexId);
            if (!CollectionUtils.isEmpty(peiLv2s)) {
                List<JingCaiPeiLv> collect = peiLv2s.stream()
                        .sorted(Comparator.comparing(JingCaiPeiLv::getDatetime).reversed())
                        .collect(Collectors.toList());
                JingCaiPeiLv peiLv2 = collect.get(0);
                peiLv.setIndexId(indexId);
                Element elementById = document.getElementById("1");
                Elements peilv = elementById.select("td");
                //初始胜
                peiLv.setSheng(Double.valueOf(peilv.get(3).text()));
                //初始平
                peiLv.setPing(Double.valueOf(peilv.get(4).text()));
                //初始负
                peiLv.setFu(Double.valueOf(peilv.get(5).text()));
                //变化胜
                peiLv.setShengChange(Double.valueOf(peilv.get(6).text()));
                //变化平
                peiLv.setPingChange(Double.valueOf(peilv.get(7).text()));
                //变化负
                peiLv.setFuChange(Double.valueOf(peilv.get(8).text()));
                peiLv.setDatetime(new Date());
                JingCaiPeiLv jingCaiPeiLv = map.get(indexId);
                if (null != jingCaiPeiLv){
                    peiLv.setR_sheng(jingCaiPeiLv.getR_sheng());
                    peiLv.setR_ping(jingCaiPeiLv.getR_ping());
                    peiLv.setR_fu(jingCaiPeiLv.getR_fu());
                    peiLv.setR_shengChange(jingCaiPeiLv.getR_shengChange());
                    peiLv.setR_pingChange(jingCaiPeiLv.getR_pingChange());
                    peiLv.setR_fuChange(jingCaiPeiLv.getR_fuChange());
                }
                jingCaiPeilvService.save(peiLv2);
            } else {
                companyType = "竞彩";
                games.forEach(game -> {
                    if (game.getIndexId().equals(indexId)) {
                        peiLv.setType(game.getType());
                        peiLv.setDate(game.getDate());
                        peiLv.setZhuDui(game.getZhuDui());
                        peiLv.setRangQiu(game.getRangQiu());
                        peiLv.setKeDui(game.getKeDui());
                        peiLv.setResult(game.getResult());
                        peiLv.setWeek(game.getWeek());
                    }
                });
                peiLv.setIndexId(indexId);
                Element elementById = document.getElementById("1");
                Elements peilv = elementById.select("td");
                //初始胜
                peiLv.setSheng(Double.valueOf(peilv.get(3).text()));
                //初始平
                peiLv.setPing(Double.valueOf(peilv.get(4).text()));
                //初始负
                peiLv.setFu(Double.valueOf(peilv.get(5).text()));
                //变化胜
                peiLv.setShengChange(Double.valueOf(peilv.get(6).text()));
                //变化平
                peiLv.setPingChange(Double.valueOf(peilv.get(7).text()));
                //变化负
                peiLv.setFuChange(Double.valueOf(peilv.get(8).text()));
                peiLv.setDatetime(new Date());
                map.put(String.valueOf(indexId), peiLv);
                jingCaiPeilvService.save(peiLv);
            }
        } else if (url.contains("rangqiu")) {
            List<JingCaiPeiLv> peiLv2s = jingCaiPeilvService.findByIndexId(indexId);
            if (!CollectionUtils.isEmpty(peiLv2s)) {
                List<JingCaiPeiLv> collect = peiLv2s.stream()
                        .sorted(Comparator.comparing(JingCaiPeiLv::getDatetime).reversed())
                        .collect(Collectors.toList());
                JingCaiPeiLv peiLv2 = collect.get(0);
                Element elementById = document.getElementById("0");
                Elements peilv = elementById.select("td");
                //初始胜
                peiLv2.setR_sheng(Double.valueOf(peilv.get(4).text()));
                //初始平
                peiLv2.setR_ping(Double.valueOf(peilv.get(5).text()));
                //初始负
                peiLv2.setR_fu(Double.valueOf(peilv.get(6).text()));
                //变化胜
                peiLv2.setR_shengChange(Double.valueOf(peilv.get(7).text()));
                //变化平
                peiLv2.setR_pingChange(Double.valueOf(peilv.get(8).text()));
                //变化负
                peiLv2.setR_fuChange(Double.valueOf(peilv.get(9).text()));
                peiLv.setDatetime(new Date());
                JingCaiPeiLv jingCaiPeiLv = map.get(indexId);
                if (null != jingCaiPeiLv){
                    peiLv.setSheng(jingCaiPeiLv.getSheng());
                    peiLv.setPing(jingCaiPeiLv.getPing());
                    peiLv.setFu(jingCaiPeiLv.getFu());
                    peiLv.setShengChange(jingCaiPeiLv.getShengChange());
                    peiLv.setPingChange(jingCaiPeiLv.getPingChange());
                    peiLv.setFuChange(jingCaiPeiLv.getFuChange());
                }
                jingCaiPeilvService.save(peiLv2);
            } else {
                games.forEach(game -> {
                    if (game.getIndexId().equals(indexId)) {
                        peiLv.setType(game.getType());
                        peiLv.setDate(game.getDate());
                        peiLv.setZhuDui(game.getZhuDui());
                        peiLv.setRangQiu(game.getRangQiu());
                        peiLv.setKeDui(game.getKeDui());
                        peiLv.setResult(game.getResult());
                        peiLv.setWeek(game.getWeek());
                    }
                });
                peiLv.setIndexId(indexId);
                Element elementById = document.getElementById("0");
                Elements peilv = elementById.select("td");
                //初始胜
                peiLv.setR_sheng(Double.valueOf(peilv.get(4).text()));
                //初始平
                peiLv.setR_ping(Double.valueOf(peilv.get(5).text()));
                //初始负
                peiLv.setR_fu(Double.valueOf(peilv.get(6).text()));
                //变化胜
                peiLv.setR_shengChange(Double.valueOf(peilv.get(7).text()));
                //变化平
                peiLv.setR_pingChange(Double.valueOf(peilv.get(8).text()));
                //变化负
                peiLv.setR_fuChange(Double.valueOf(peilv.get(9).text()));
                peiLv.setDatetime(new Date());
                map.put(String.valueOf(indexId), peiLv);
                jingCaiPeilvService.save(peiLv);
            }
        }

    }

    private Site site = Site.me()
            .setCharset("gb2312")
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
            .addCookie("cookie", cookie)
            .setTimeOut(3000)
            .setRetrySleepTime(1000)
            .setRetryTimes(3);

    @Override
    public Site getSite() {
        return site;
    }

}
